package com.springboot.springbootlogindemo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.springbootlogindemo.domain.Singer;
import com.springboot.springbootlogindemo.mapper.SingerMapper;
import com.springboot.springbootlogindemo.service.SingerService;

import com.springboot.springbootlogindemo.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 歌手service实现类
 */
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Resource
    private SingerMapper singerMapper;


    /**
     * 增加歌手
     * @param singer
     * @return
     */
    @Override
    public Result addSinger(Singer singer) {
        Singer one = query().eq("name", singer.getName()).one();
        if(one!=null){
            return Result.error("歌手已经存在，请勿重复添加");
        }
        return Result.success(singer,"歌手添加成功");
    }

    /**
     * 修改歌手信息
     * @param singer
     * @return
     */
    @Override
    public Result updateSinger(Singer singer) {

        return singerMapper.updateById(singer)>0?Result.success(singer,"歌手信息已修改"):Result.error("歌手信息修改错误");
    }

    /**
     * 删除歌手
     * @param singer
     * @return
     */
    @Override
    public Result deleteSinger(Singer singer) {
        return singerMapper.deleteById(singer.getId())>0?Result.success("歌手删除成功"):Result.error("歌手删除失败");
    }

    /**
     * 获取所有歌手信息
     * @return
     */
    @Override
    public Result getAll() {
        List<Singer> list = query().list();
        return  Result.success(list);
    }

    /**
     * 根据姓名查找歌手
     * @param name
     * @return
     */
    @Override
    public Result findSignerByName(String name) {
        List<Singer> list = query().eq("name", name).list();
        return  Result.success(list);
    }
}
